package com.example.gaojt.androiddemo.statical.sdk;

import android.content.Context;
import android.text.TextUtils;

import com.example.gaojt.androiddemo.statical.DemoActivity;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

/**
 * Created by allen on 17-3-6.
 */

public final class StatisticsApi {

    private static final String STATISTICAL_TEST_NEW = "https://10.86.236.173:8089/api/cgs/log";
    private static final String STATISTICAL_RELEASE_NEW_CHINA = "https://dg.udc.cn.tclclouds.com/api/cgs/log";
    private static final String STATISTICAL_RELEASE_NEW_RUSSIA = "https://dg.udc.ru.tclclouds.com/api/cgs/log";
    private static final String STATISTICAL_RELEASE_NEW_EUROPE = "https://dg.udc.eu.tclclouds.com/api/cgs/log";
    private static final String STATISTICAL_RELEASE_NEW_NORTHAMERICA = "https://dg.udc.us.tclclouds.com/api/cgs/log";


    private static StatisticsManager sManager;

    public static void aa(DemoActivity activity) {

    }

    public static class Config {
        public String appKey;
        public String customProjectName;
        public String channel;
        public String versionName;
    }

    public static void init(Context context, Config config, boolean debug) {
        String processName = getProcessName(android.os.Process.myPid());
        sManager = new StatisticsManager(context, processName, config, debug);
    }

    public static void init(Context context, String processName, Config config, boolean debug) {
        sManager = new StatisticsManager(context, processName, config, debug);
    }

    // 获取当前进程名的工具方法
    public static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 新增onEvent方法,含map参数
     *
     * @param eventName：事件名称，必须唯一
     * @param map：事件统计包含参数
     */
    public static void onEvent(final String eventName, final Map<String, String> map) {
        sManager.onEvent(eventName, map);
    }

    /**
     * 新增onEvent方法
     *
     * @param eventName
     */
    public static void onEvent(final String eventName) {
        sManager.onEvent(eventName);
    }
}
